package com.mall.service.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.UserLockService;
import com.mall.custom.dao.read.UserLockReadMapper;
import com.mall.custom.dao.write.UserLockWriteMapper;
import com.mall.pojo.UserLock;
import com.mall.pojo.UserLockExample;
import com.mall.pojo.UserLockExample.Criteria;
@Service
public class UserLockServiceImpl implements UserLockService {
	@Autowired
	private UserLockWriteMapper userLockWriteMapper;
	@Autowired
	private UserLockReadMapper userLockReadMapper;


	@Override
	public int insertUserLock(UserLock userLock) {
		userLock.setGmtModify(new Date());
		userLock.setGmtCreate(new Date());
		return this.userLockWriteMapper.insert(userLock);
	}

	@Override
	public int modifyUserLock(UserLock userLock) {
		userLock.setGmtModify(new Date());
		return this.userLockWriteMapper.updateByPrimaryKey(userLock);
	}

	@Override
	public UserLock getUserLockByMallItemIdAndOpendIdAndShareUserId(Long mallItemId, String openId, Long shareUserId) {
		UserLockExample example = new UserLockExample();
		example.setOrderByClause("gmt_modify desc");
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(mallItemId);
		criteria.andOpenIdEqualTo(openId);
		if (shareUserId != null) {
			criteria.andShareUserIdEqualTo(shareUserId);
		}
		List<UserLock> userLockList =  this.userLockReadMapper.selectByExample(example);
		UserLock userLock = null;
		if(userLockList !=null && userLockList.size()>0){
			userLock = userLockList.get(0);
		}
		return userLock;
	}

}
